<template>
	<el-dialog
		title="角色修改"
		:visible.sync="childNoneDialog"
		:close-on-click-modal="false"
		:close-on-press-escape="false"
		:before-close="cancelChildNoneDialog"
	>
		<el-form ref="form" :model="updateDatas" :rules="rules" label-width="80px">
			<el-form-item label="名称" prop="roleName">
				<el-input v-model="updateDatas.roleName"> </el-input>
			</el-form-item>
			<el-form-item label="是否启用">
				<el-switch
					v-model="updateDatas.whetherEnable"
					active-value="0"
					inactive-value="1"
				></el-switch>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" size="small" @click="updateChild('form')"
					>保存</el-button
				>
				<el-button size="small" @click="cancelChildNoneDialog">取消</el-button>
			</el-form-item>
		</el-form>
	</el-dialog>
</template>

<script>
import { roleUpdate, roleNameCheck } from '@/http/api.js'
export default {
	name: 'Compile',
	props: {
		childNoneDialog: {
			type: Boolean,
			default: false,
		},
		updateData: {
			type: Object,
			default: () => {
				return {}
			},
		},
		compileData: {
			type: Object,
			default: () => {
				return {}
			},
		},
	},
	data() {
		var compileValidator = this.SpCodeFun
		return {
			compileDatas: this.compileData,
			rules: {
				roleName: [
					{ required: true, message: '不能为空', trigger: 'blur' },
					{ validator: compileValidator, trigger: 'blur' },
				],
			},
		}
	},
	computed: {
		updateDatas() {
			return this.updateData
		},
	},
	methods: {
		updateChild(formName) {
			this.$refs[formName].validate(valid => {
				if (valid) {
					roleUpdate({
						id: this.updateData.id,
						roleName: this.updateData.roleName,
						whetherEnable: this.updateData.whetherEnable,
					}).then(data => {
						if (data.data.head.code == 200) {
							this.$message({
								message: '保存成功',
								type: 'success',
							})
						}
					})
					let dialogValue = {
						verdict: false,
						typeValue: 1,
					}
					this.$emit('compileClick', dialogValue)
				} else {
					// this.$message.error('保存失败')
					return false
				}
			})
		},
		cancelChildNoneDialog() {
			let dialogValue = {
				verdict: false,
				typeValue: 0,
			}
			this.$emit('compileClick', dialogValue)
			this.$nextTick(() => {
				this.$refs['form'].clearValidate()
			})
		},
		// 编辑查重
		SpCodeFun(rule, value, callback) {
			roleNameCheck({
				parentUuid: this.updateData.parentUuid,
				roleName: value,
				id: this.updateData.id,
			})
				.then(({ data }) => {
					if (data.head.code == 200) {
						callback()
					}
				})
				.catch(({ data }) => {
					if (data.head.code == 400) {
						callback(new Error('账号不能重复'))
						return
					} else {
						callback()
					}
				})
		},
	},
}
</script>

<style></style>
